package com.runa.persistence.service;

import com.runa.persistence.CollectionUtil;
import com.runa.persistence.entity.EntityThirdPart;
import com.runa.persistence.entity.EntityThirdPartParam;
import com.runa.persistence.repository.ThirdPartParamRepository;
import com.runa.persistence.repository.ThirdPartRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 2023/8/8 13:57
 * zhangYB
 */
@Slf4j
@Service
public class ThirdPartService {

    @Resource
    private ThirdPartRepository thirdPartRepository;
    @Resource
    private ThirdPartParamRepository thirdPartParamRepository;

    public EntityThirdPart getThirdPartByCode(String code) {
        List<EntityThirdPart> thirdParts = thirdPartRepository.findByCode(code);
        if (CollectionUtil.isEmpty(thirdParts)) {
            return null;
        }
        return thirdParts.get(0);
    }

    public EntityThirdPart getThirdPart(String thirdPartId) {
        Optional<EntityThirdPart> optional = thirdPartRepository.findById(Integer.valueOf(thirdPartId));
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    public Map<String, String> getThirdPartParamMap(String thirdPartId) {
        List<EntityThirdPartParam> entityThirdPartParams = thirdPartParamRepository.findByCode(thirdPartId);
        return entityThirdPartParams.stream().collect(Collectors.toMap(EntityThirdPartParam::getName, e -> e.getVal(), (e1, e2) -> e1));
    }









}
